
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>OTP并发 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="otp-supervisors.html" />
    
    
    <link rel="prev" href="concurrency.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">第一部分：基础</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../basics/basics.html">
            
                <a href="../basics/basics.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../basics/collections.html">
            
                <a href="../basics/collections.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../basics/enum.html">
            
                <a href="../basics/enum.html">
            
                    
                    Enum 模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../basics/pattern-matching.html">
            
                <a href="../basics/pattern-matching.html">
            
                    
                    模式匹配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../basics/control-structures.html">
            
                <a href="../basics/control-structures.html">
            
                    
                    控制语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../basics/functions.html">
            
                <a href="../basics/functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../basics/pipe-operator.html">
            
                <a href="../basics/pipe-operator.html">
            
                    
                    管道操作符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../basics/modules.html">
            
                <a href="../basics/modules.html">
            
                    
                    模块(Module)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../basics/mix.html">
            
                <a href="../basics/mix.html">
            
                    
                    Mix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../basics/sigils.html">
            
                <a href="../basics/sigils.html">
            
                    
                    魔符(Sigil)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../basics/documentation.html">
            
                <a href="../basics/documentation.html">
            
                    
                    文档模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../basics/testing.html">
            
                <a href="../basics/testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../basics/comprehensions.html">
            
                <a href="../basics/comprehensions.html">
            
                    
                    推导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../basics/strings.html">
            
                <a href="../basics/strings.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../basics/date-time.html">
            
                <a href="../basics/date-time.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../basics/mix-tasks.html">
            
                <a href="../basics/mix-tasks.html">
            
                    
                    自定义Mix任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../basics/iex-helpers.html">
            
                <a href="../basics/iex-helpers.html">
            
                    
                    IEx辅助函数
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第二部分：高级</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="erlang.html">
            
                <a href="erlang.html">
            
                    
                    和Erlang互操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="error-handling.html">
            
                <a href="error-handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="escripts.html">
            
                <a href="escripts.html">
            
                    
                    可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="concurrency.html">
            
                <a href="concurrency.html">
            
                    
                    并发
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="2.5" data-path="otp-concurrency.html">
            
                <a href="otp-concurrency.html">
            
                    
                    OTP并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="otp-supervisors.html">
            
                <a href="otp-supervisors.html">
            
                    
                    OTP Supervisors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="otp-distribution.html">
            
                <a href="otp-distribution.html">
            
                    
                    OTP 分布式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="metaprogramming.html">
            
                <a href="metaprogramming.html">
            
                    
                    元编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="umbrella-projects.html">
            
                <a href="umbrella-projects.html">
            
                    
                    Umbrella Projects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="typespec.html">
            
                <a href="typespec.html">
            
                    
                    Specifications and types
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="behaviours.html">
            
                <a href="behaviours.html">
            
                    
                    行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="gen-stage.html">
            
                <a href="gen-stage.html">
            
                    
                    GenStage
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="protocols.html">
            
                <a href="protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="nerves.html">
            
                <a href="nerves.html">
            
                    
                    Nerves
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第三部分：ECTO</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../ecto/basics.html">
            
                <a href="../ecto/basics.html">
            
                    
                    Basics
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../ecto/changesets.html">
            
                <a href="../ecto/changesets.html">
            
                    
                    Changesets
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../ecto/associations.html">
            
                <a href="../ecto/associations.html">
            
                    
                    关联关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../ecto/querying.html">
            
                <a href="../ecto/querying.html">
            
                    
                    查询
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第四部分：专题</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../specifics/plug.html">
            
                <a href="../specifics/plug.html">
            
                    
                    Plug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../specifics/eex.html">
            
                <a href="../specifics/eex.html">
            
                    
                    嵌入的 Elixir(EEx)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../specifics/ets.html">
            
                <a href="../specifics/ets.html">
            
                    
                    Erlang 项式存储(ETS)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../specifics/mnesia.html">
            
                <a href="../specifics/mnesia.html">
            
                    
                    Mnesia 数据库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../specifics/debugging.html">
            
                <a href="../specifics/debugging.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第五部分：程序库</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../libraries/guardian.html">
            
                <a href="../libraries/guardian.html">
            
                    
                    Guardian(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../libraries/poolboy.html">
            
                <a href="../libraries/poolboy.html">
            
                    
                    Poolboy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../libraries/benchee.html">
            
                <a href="../libraries/benchee.html">
            
                    
                    Benchee
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../libraries/bypass.html">
            
                <a href="../libraries/bypass.html">
            
                    
                    Bypass
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../libraries/distillery.html">
            
                <a href="../libraries/distillery.html">
            
                    
                    Distillery(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../libraries/stream-data.html">
            
                <a href="../libraries/stream-data.html">
            
                    
                    StreamData
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >OTP并发</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x770B;&#x8FC7;&#x4E86; Elixir &#x5C42;&#x7684;&#x5E76;&#x53D1;&#x62BD;&#x8C61;&#x673A;&#x5236;&#xFF0C;&#x4F46;&#x6709;&#x65F6;&#x5019;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x66F4;&#x591A;&#x7684;&#x63A7;&#x5236;&#xFF0C;&#x90A3;&#x5C31;&#x8981;&#x4E86;&#x89E3; Elixir &#x5E95;&#x5C42;&#x7684;&#x4E1C;&#x897F;&#xFF1A;OTP &#x884C;&#x4E3A;&#xFF08;behaviors&#xFF09;&#x3002;</p>
<p>&#x8FD9;&#x8282;&#x8BFE;&#xFF0C;&#x6211;&#x4EEC;&#x4E3B;&#x8981;&#x8BB2;&#x4E24;&#x4E2A;&#x4E1C;&#x897F;&#xFF1A;Genservers&#x3002;</p>
<h1 id="genserver">GenServer</h1>
<p>OTP server &#x662F;&#x4E00;&#x4E2A;&#x6A21;&#x5757;&#xFF0C;&#x5305;&#x542B;&#x4E86; Genserver &#x7684;&#x4E3B;&#x8981;&#x884C;&#x4E3A;&#xFF0C;&#x5916;&#x52A0;&#x4E00;&#x7CFB;&#x5217;&#x7684; callbacks&#x3002;Genserver &#x6700;&#x6838;&#x5FC3;&#x7684;&#x5185;&#x5BB9;&#x662F;&#x8FD9;&#x6837;&#x4E00;&#x4E2A;&#x5FAA;&#x73AF;&#xFF1A;&#x6BCF;&#x6B21;&#x8FED;&#x4EE3;&#x5904;&#x7406;&#x4E00;&#x4E2A;&#x5E26;&#x6709;&#x76EE;&#x6807;&#x72B6;&#x6001;&#x7684;&#x8BF7;&#x6C42;&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x6F14;&#x793A; Genserver &#x7684; API&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684; queue&#xFF0C;&#x6765;&#x5B58;&#x50A8;&#x548C;&#x83B7;&#x53D6;&#x503C;&#x3002;</p>
<p>&#x6700;&#x5F00;&#x59CB;&#xFF0C;&#x6211;&#x4EEC;&#x8981;&#x5148;&#x542F;&#x52A8;&#x548C;&#x521D;&#x59CB;&#x5316; Genserver&#xFF0C;&#x4E00;&#x822C;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x94FE;&#x63A5;&#x8FDB;&#x7A0B;&#xFF0C;&#x6240;&#x4EE5;&#x8FD8;&#x8981;&#x4F7F;&#x7528; <code>Genserver.start_link/3</code>&#x3002;&#x4F20;&#x9012;&#x7ED9; Genserver &#x7684;&#x53C2;&#x6570;&#x5305;&#x62EC;&#xFF1A;&#x6211;&#x4EEC;&#x6240;&#x5728;&#x7684;&#x6A21;&#x5757;&#xFF0C;&#x521D;&#x59CB;&#x72B6;&#x6001;&#xFF0C;&#x4EE5;&#x53CA;&#x5176;&#x4ED6; Genserver &#x53C2;&#x6570;&#x3002;<code>Genserver.init/1</code> &#x7684;&#x53C2;&#x6570;&#x7528;&#x6765;&#x8BBE;&#x7F6E;&#x521D;&#x59CB;&#x7684;&#x72B6;&#x6001;&#xFF0C;&#x6BD4;&#x5982;&#x5728;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x521D;&#x59CB;&#x72B6;&#x6001;&#x4E3A; []:</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">SimpleQueue</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> GenServer

  <span class="hljs-variable">@doc</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-string">&quot;
  Start our queue and link it.  This is a helper function
  &quot;</span><span class="hljs-string">&quot;&quot;</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">start_link</span></span>(state \\ []) <span class="hljs-keyword">do</span>
    GenServer.start_link(__MODULE_<span class="hljs-number">_</span>, state, <span class="hljs-symbol">name:</span> __MODULE_<span class="hljs-number">_</span>)
  <span class="hljs-keyword">end</span>

  <span class="hljs-variable">@doc</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-string">&quot;
  GenServer.init/1 callback
  &quot;</span><span class="hljs-string">&quot;&quot;</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">init</span></span>(state), <span class="hljs-symbol">do:</span> {<span class="hljs-symbol">:ok</span>, state}
<span class="hljs-keyword">end</span>
</code></pre>
<h2 id="&#x540C;&#x6B65;&#x51FD;&#x6570;">&#x540C;&#x6B65;&#x51FD;&#x6570;</h2>
<p>&#x6709;&#x65F6;&#x5019;&#x9700;&#x8981;&#x548C; Genservers &#x8FDB;&#x884C;&#x540C;&#x6B65;&#x7684;&#x4EA4;&#x4E92;&#xFF1A;&#x8C03;&#x7528;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x7136;&#x540E;&#x7B49;&#x5F85;&#x5B83;&#x7684;&#x54CD;&#x5E94;&#x8FD4;&#x56DE;&#x3002;&#x8981;&#x5904;&#x7406;&#x540C;&#x6B65;&#x8BF7;&#x6C42;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5B9E;&#x73B0; <code>Genserver.handle_call/3</code> &#x51FD;&#x6570;&#xFF0C;&#x63A5;&#x53D7;&#x7684;&#x53C2;&#x6570;&#x662F;&#xFF1A;&#x8BF7;&#x6C42;&#x3001;&#x8C03;&#x7528;&#x8005;&#x7684; PID&#xFF0C;&#x521D;&#x59CB;&#x7684;&#x72B6;&#x6001;&#xFF0C;&#x671F;&#x671B;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x662F; <code>{:reply, response, state}</code> &#x4E09;&#x5143;&#x7EC4;&#x3002;</p>
<p>&#x4F7F;&#x7528;&#x6A21;&#x5F0F;&#x5339;&#x914D;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4E3A;&#x4E0D;&#x540C;&#x7684;&#x8BF7;&#x6C42;&#x548C;&#x72B6;&#x6001;&#x5B9A;&#x4E49;&#x4E0D;&#x540C;&#x7684; callbacks&#xFF0C;&#x80FD;&#x591F;&#x63A5;&#x53D7;&#x7684;&#x6240;&#x6709;&#x8FD4;&#x56DE;&#x503C;&#x5217;&#x8868;&#x53EF;&#x4EE5;&#x524D;&#x5F80; <a href="https://hexdocs.pm/elixir/GenServer.html#c:handle_call/3" target="_blank"><code>Genserver.handle_call/3</code></a> &#x6587;&#x6863;&#x5904;&#x67E5;&#x770B;&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x6F14;&#x793A;&#x540C;&#x6B65;&#x8BF7;&#x6C42;&#xFF0C;&#x6211;&#x4EEC;&#x6DFB;&#x52A0;&#x8FD9;&#x6837;&#x7684;&#x529F;&#x80FD;&#xFF1A;&#x8FD4;&#x56DE;&#x73B0;&#x5728;&#x961F;&#x5217;&#x7684;&#x72B6;&#x6001;&#x4EE5;&#x53CA;&#x5220;&#x9664;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x4E00;&#x4E2A;&#x503C;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">SimpleQueue</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> GenServer

  <span class="hljs-comment">### GenServer API</span>

  <span class="hljs-variable">@doc</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-string">&quot;
  GenServer.init/1 callback
  &quot;</span><span class="hljs-string">&quot;&quot;</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">init</span></span>(state), <span class="hljs-symbol">do:</span> {<span class="hljs-symbol">:ok</span>, state}

  <span class="hljs-variable">@doc</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-string">&quot;
  GenServer.handle_call/3 callback
  &quot;</span><span class="hljs-string">&quot;&quot;</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">handle_call</span></span>(<span class="hljs-symbol">:dequeue</span>, _from, [value | state]) <span class="hljs-keyword">do</span>
    {<span class="hljs-symbol">:reply</span>, value, state}
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">handle_call</span></span>(<span class="hljs-symbol">:dequeue</span>, _from, []), <span class="hljs-symbol">do:</span> {<span class="hljs-symbol">:reply</span>, <span class="hljs-keyword">nil</span>, []}

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">handle_call</span></span>(<span class="hljs-symbol">:queue</span>, _from, state), <span class="hljs-symbol">do:</span> {<span class="hljs-symbol">:reply</span>, state, state}

  <span class="hljs-comment">### Client API / Helper functions</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">start_link</span></span>(state \\ []) <span class="hljs-keyword">do</span>
    GenServer.start_link(__MODULE_<span class="hljs-number">_</span>, state, <span class="hljs-symbol">name:</span> __MODULE_<span class="hljs-number">_</span>)
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">queue</span></span>, <span class="hljs-symbol">do:</span> GenServer.call(__MODULE_<span class="hljs-number">_</span>, <span class="hljs-symbol">:queue</span>)
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">dequeue</span></span>, <span class="hljs-symbol">do:</span> GenServer.call(__MODULE_<span class="hljs-number">_</span>, <span class="hljs-symbol">:dequeue</span>)
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x6765;&#x6D4B;&#x8BD5;&#x4E00;&#x4E0B; SimpleQueue &#x521A;&#x5B8C;&#x6210;&#x7684; dequeue &#x529F;&#x80FD;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; SimpleQueue.start_link([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>])
{<span class="hljs-symbol">:ok</span>, <span class="hljs-comment">#PID&lt;0.90.0&gt;}</span>
iex&gt; SimpleQueue.dequeue
<span class="hljs-number">1</span>
iex&gt; SimpleQueue.dequeue
<span class="hljs-number">2</span>
iex&gt; SimpleQueue.queue
[<span class="hljs-number">3</span>]
</code></pre>
<h2 id="&#x5F02;&#x6B65;&#x51FD;&#x6570;">&#x5F02;&#x6B65;&#x51FD;&#x6570;</h2>
<p><code>handle_cast/2</code> &#x662F;&#x5904;&#x7406;&#x5F02;&#x6B65;&#x51FD;&#x6570;&#x7684;&#xFF0C;  &#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x548C; <code>handle_call/3</code> &#x7684;&#x7528;&#x6CD5;&#x4E00;&#x6837;&#xFF0C;&#x9664;&#x4E86;&#x5B83;&#x4E0D;&#x63A5;&#x53D7;&#x8C03;&#x7528;&#x8005;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x800C;&#x4E14;&#x6CA1;&#x6709;&#x8FD4;&#x56DE;&#x503C;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x628A; enqueue &#x529F;&#x80FD;&#x8BBE;&#x8BA1;&#x6210;&#x5F02;&#x6B65;&#x7684;&#xFF1A;&#x66F4;&#x65B0; queue &#x7684;&#x5185;&#x5BB9;&#xFF0C;&#x4F46;&#x5E76;&#x4E0D;&#x963B;&#x585E;&#x5F53;&#x524D;&#x7A0B;&#x5E8F;&#x7684;&#x8FD0;&#x884C;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">SimpleQueue</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> GenServer

  <span class="hljs-comment">### GenServer API</span>

  <span class="hljs-variable">@doc</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-string">&quot;
  GenServer.init/1 callback
  &quot;</span><span class="hljs-string">&quot;&quot;</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">init</span></span>(state), <span class="hljs-symbol">do:</span> {<span class="hljs-symbol">:ok</span>, state}

  <span class="hljs-variable">@doc</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-string">&quot;
  GenServer.handle_call/3 callback
  &quot;</span><span class="hljs-string">&quot;&quot;</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">handle_call</span></span>(<span class="hljs-symbol">:dequeue</span>, _from, [value | state]) <span class="hljs-keyword">do</span>
    {<span class="hljs-symbol">:reply</span>, value, state}
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">handle_call</span></span>(<span class="hljs-symbol">:dequeue</span>, _from, []), <span class="hljs-symbol">do:</span> {<span class="hljs-symbol">:reply</span>, <span class="hljs-keyword">nil</span>, []}

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">handle_call</span></span>(<span class="hljs-symbol">:queue</span>, _from, state), <span class="hljs-symbol">do:</span> {<span class="hljs-symbol">:reply</span>, state, state}

  <span class="hljs-variable">@doc</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-string">&quot;
  GenServer.handle_cast/2 callback
  &quot;</span><span class="hljs-string">&quot;&quot;</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">handle_cast</span></span>({<span class="hljs-symbol">:enqueue</span>, value}, state) <span class="hljs-keyword">do</span>
    {<span class="hljs-symbol">:noreply</span>, state ++ [value]}
  <span class="hljs-keyword">end</span>

  <span class="hljs-comment">### Client API / Helper functions</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">start_link</span></span>(state \\ []) <span class="hljs-keyword">do</span>
    GenServer.start_link(__MODULE_<span class="hljs-number">_</span>, state, <span class="hljs-symbol">name:</span> __MODULE_<span class="hljs-number">_</span>)
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">queue</span></span>, <span class="hljs-symbol">do:</span> GenServer.call(__MODULE_<span class="hljs-number">_</span>, <span class="hljs-symbol">:queue</span>)
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">enqueue</span></span>(value), <span class="hljs-symbol">do:</span> GenServer.cast(__MODULE_<span class="hljs-number">_</span>, {<span class="hljs-symbol">:enqueue</span>, value})
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">dequeue</span></span>, <span class="hljs-symbol">do:</span> GenServer.call(__MODULE_<span class="hljs-number">_</span>, <span class="hljs-symbol">:dequeue</span>)
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x73B0;&#x5728;&#x4F7F;&#x7528;&#x4E00;&#x4E0B;&#x8FD9;&#x4E2A;&#x65B0;&#x529F;&#x80FD;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; SimpleQueue.start_link([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>])
{<span class="hljs-symbol">:ok</span>, <span class="hljs-comment">#PID&lt;0.100.0&gt;}</span>
iex&gt; SimpleQueue.queue
[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
iex&gt; SimpleQueue.enqueue(<span class="hljs-number">20</span>)
<span class="hljs-symbol">:ok</span>
iex&gt; SimpleQueue.queue
[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">20</span>]
</code></pre>
<p>&#x53EF;&#x4EE5;&#x524D;&#x5F80;&#x5B98;&#x65B9;&#x7684; <a href="https://hexdocs.pm/elixir/GenServer.html#content" target="_blank"><code>GenServer</code></a> &#x6587;&#x6863;&#x4E86;&#x89E3;&#x66F4;&#x591A;&#x7684;&#x4FE1;&#x606F;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="concurrency.html" class="navigation navigation-prev " aria-label="Previous page: 并发">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="otp-supervisors.html" class="navigation navigation-next " aria-label="Next page: OTP Supervisors">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"version":"1.0.3","title":"OTP并发","level":"2.5","depth":1,"next":{"title":"OTP Supervisors","level":"2.6","depth":1,"path":"lessons/advanced/otp-supervisors.md","ref":"lessons/advanced/otp-supervisors.md","articles":[]},"previous":{"title":"并发","level":"2.4","depth":1,"path":"lessons/advanced/concurrency.md","ref":"lessons/advanced/concurrency.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"lessons/advanced/otp-concurrency.md","mtime":"2019-11-05T02:25:51.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-11-05T03:01:22.004Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

